home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / objtools / fitobj.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  144 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /* 
  18.  *    fitobj -
  19.  *        Fit a sgi object into a unit cube.
  20.  *
  21.  *            Paul Haeberli - 1990
  22.  */
  23. #include "stdio.h"
  24. #include "math.h"
  25. #include "vect.h"
  26. #include "sgiobj.h"
  27.  
  28. main(argc,argv)
  29. int argc;
  30. char **argv;
  31. {
  32.     sgiobj *obj;
  33.  
  34.     if(argc<3) {
  35.     fprintf(stderr,"fitobj: in.bin out.bin\n");
  36.     exit(1);
  37.     }
  38.     obj = readsgiobj(argv[1]);
  39.     fitobj(obj,-0.5,0.5,-0.5,0.5,-0.5,0.5);
  40.     writesgiobj(argv[2],obj);
  41.     exit(0);
  42. }
  43.  
  44. float oxmin, oxmax;
  45. float oymin, oymax;
  46. float ozmin, ozmax;
  47. float xavg, yavg, zavg;
  48. float xmid, ymid, zmid;
  49. float sc;
  50.  
  51. findmax(fptr)
  52. float *fptr;
  53. {
  54.     fptr += OFFSET_POINT;
  55.     if(*fptr<oxmin)
  56.      oxmin = *fptr;
  57.     if(*fptr>oxmax)
  58.      oxmax = *fptr;
  59.     fptr++;
  60.     if(*fptr<oymin)
  61.      oymin = *fptr;
  62.     if(*fptr>oymax)
  63.      oymax = *fptr;
  64.     fptr++;
  65.     if(*fptr<ozmin)
  66.      ozmin = *fptr;
  67.     if(*fptr>ozmax)
  68.      ozmax = *fptr;
  69. }
  70.  
  71. scalepnt(fptr)
  72. float *fptr;
  73. {
  74.     fptr += OFFSET_POINT;
  75.     *fptr = xavg+(sc*(*fptr-xmid));
  76.     fptr++;
  77.     *fptr = yavg+(sc*(*fptr-ymid));
  78.     fptr++;
  79.     *fptr = zavg+(sc*(*fptr-zmid));
  80. }
  81.  
  82. fitobj(obj,xmin,xmax,ymin,ymax,zmin,zmax)
  83. sgiobj *obj;
  84. float xmin,xmax,ymin,ymax,zmin,zmax;
  85. {
  86.     float delx, dely, delz;
  87.     float odelx, odely, odelz;
  88.     float tdelx, tdely, tdelz;
  89.     float *fptr;
  90.     int npoints, mode;
  91.  
  92.     delx = xmax-xmin;
  93.     dely = ymax-ymin;
  94.     delz = zmax-zmin;
  95.  
  96.     oxmin = oymin = ozmin =  1000.0;
  97.     oxmax = oymax = ozmax = -1000.0;
  98.  
  99.     applytoverts(obj,findmax);
  100.  
  101.     tdelx = odelx = oxmax-oxmin;
  102.     tdely = odely = oymax-oymin;
  103.     tdelz = odelz = ozmax-ozmin;
  104.  
  105.     tdelx /= delx;
  106.     tdely /= dely;
  107.     tdelz /= delz;
  108.     if(tdelx>tdely) {
  109.     if(tdelx>tdelz) {
  110.         mode = 1;
  111.     } else {
  112.         mode = 3;
  113.     }
  114.     } else {
  115.     if(tdely>tdelz) {
  116.         mode = 2;
  117.     } else {
  118.         mode = 3;
  119.     }
  120.     }
  121.     switch(mode) {
  122.     case 1:
  123.         sc = delx/odelx;
  124.         break;
  125.     case 2:
  126.         sc = dely/odely;
  127.         break;
  128.     case 3:
  129.         sc = delz/odelz;
  130.         break;
  131.     }
  132.  
  133.     npoints = obj->nlongs/PNTLONGS;
  134.     fptr = (float *)obj->data;
  135.     fptr+= OFFSET_POINT;
  136.     xmid = (oxmin+oxmax)/2.0;
  137.     ymid = (oymin+oymax)/2.0;
  138.     zmid = (ozmin+ozmax)/2.0;
  139.     xavg = (xmin+xmax)/2.0;
  140.     yavg = (ymin+ymax)/2.0;
  141.     zavg = (zmin+zmax)/2.0;
  142.     applytoverts(obj,scalepnt);
  143. }
  144.